define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;

    $(document).on("change", ".js_identity", function() {
        var type = $(this).find("option:selected").data("type");
        $(".js_type_list").addClass('hide');
        $(".js_" + type).removeClass('hide');
    })
    $(document).on("change", "select.js_select_distri", function(e) {
        var $this = $(this),
            val = $this.val(),
            $lower = $("select.js_merchant"),
            type = $this.find("option:selected").data("type");
        if (type == "supplier") {

        } else {
            $lower.data("ruleRequired", false);
        }
        loadLower($lower, val);
    });
    $(document).on("change", "select.js_select_dis_sales", function(e) {
        var $this = $(this),
            val = $this.val(),
            $p=$this.closest('.js_type_list');
        loadLower($("select.js_merchant_sales",$p), val);
        loadLower($("select.js_sale_cascade",$p), val);
    });
    $(document).on("change", "select.js_merchant_sales", function(e) {
        var $this = $(this),
            val = $this.val(),
            $p=$this.closest('.js_type_list');
        loadLower($("select.js_sale_cascade",$p), val);
    });

    function loadLower(obj, val) {
        obj.data("pid", val);
        obj.empty();
        obj.select2();
        var t = obj,
            n = {},
            c = t.data('maxcount'),
            o = t.data('chang'),
            r = t.data('remote') || t.attr('href'),
            pid = t.data("pid"),
            required_msg = t.data("msgRequired") || t.data("placeholder");
        c && (n.maximumSelectionSize = c);
        if (o) {
            switch (o) {
                case "submit":
                    {
                        t.on("change", function(e) {
                            t.closest("form ").submit();
                        })
                    }
            }
        };
        if (r) {
            if (pid != undefined) {
                r = r.format(pid);
            }
            t.select2({
                ajax: {
                    url: r,
                    dataType: 'json',
                    delay: 500,
                    data: function(params) {
                        return {
                            q: params.term, // search term
                            page: params.page || 1
                        };
                    },
                    processResults: function(data, params) {
                        if (config.issucceed(data)) {
                            params.page = params.page || 1;
                            if (params.page == 1) {
                                data.items.unshift({
                                    "id": "js_select2_4placeholder",
                                    "value": t.data("placeholder")
                                });
                            }

                            return {
                                results: data.items,
                                pagination: {
                                    more: (params.page * data.page_num) < data.total_count
                                }
                            };
                        } else {
                            config.msg.error(data.message || config.lang.sendError);
                        }
                    },
                    cache: true
                },
                escapeMarkup: function(markup) {
                    return markup;
                },
                minimumInputLength: 0,
                templateResult: formatRepo,
                templateSelection: formatRepoSelection
            });
        } else {
            t.select2(n)
        }
        t.change(function() {
            var $this = $(this);
            if ($this.val() == "js_select2_4placeholder") {
                $this.find("option:selected").attr("value", "");
            }
        });
    }
    $(document).on("click", ".js_get_code", function() {
        var $this = $(this),
            path = $this.data("path"),
            js_phone_number = $(".js_phone_number").val();
        if (js_phone_number) {
            $this.attr("disabled", "disabled");
            var max = 59000;
            $this.text("60秒后重新获取")
            var t1 = setInterval(function() {
                $this.text("{0}秒后重新获取".format(max / 1000))
                max -= 1000;
                if (max == 0) {
                    $this.text("获取验证码").removeAttr('disabled')
                    clearInterval(t1);
                };
            }, 1000)
            method.postd(path, function(d) {
                if (config.issucceed(d)) {
                    config.msg.info(d.message || config.lang.sendSuccess);
                } else {
                    config.msg.error(d.message || config.lang.sendError);
                }
            }, {
                phone: js_phone_number
            });
        } else {
            config.msg.error("请填写手机号码");
        }
    });

    function formatRepo(repo) {
        if (repo.loading) return repo.text;

        var markup = '<li>' + repo.value +
            '</li>';

        return markup;
    }

    function formatRepoSelection(repo) {
        return repo.value || repo.text;
    }
    $(document).on("click", '.js_client_submit', function() {
        var $form = $(this).closest('form');
        var $select2 = $('[data-toggle="select2_4"]', $form),
            flag = true;
        $select2.each(function() {
            var t = $(this),
                val = t.val(),
                required = t.data("ruleRequired"),
                required_msg = t.data("msgRequired") || t.data("placeholder"),
                $p = t.closest("div");
            if (required && !$p.is(":hidden")) {
                if (!val) {
                    config.msg.info(required_msg || "请输入必填项");
                    flag = false;
                }

            };
        });
        return flag;
    })
});